package com.jeeplus.test.website.websiteNotice.service.Impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.test.tools.resp.Convert2Bean;
import com.jeeplus.test.website.websiteNotice.domain.entity.WebNotice;
import com.jeeplus.test.website.websiteNotice.domain.vo.WebNoticeReqVo;
import com.jeeplus.test.website.websiteNotice.domain.vo.WebNoticeRespVo;
import com.jeeplus.test.website.websiteNotice.mapper.WebNoticeMapper;
import com.jeeplus.test.website.websiteNotice.service.WebNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WebNoticeServiceImpl extends ServiceImpl<WebNoticeMapper, WebNotice> implements WebNoticeService {

    @Autowired
    private WebNoticeMapper webNoticeMapper;
    @Autowired
    private UserService userService;

    @Override
    public Page<WebNoticeRespVo> getNoticePage(WebNoticeReqVo webNoticeReqVo, long current, long size) {
        WebNotice webNotice = Convert2Bean.convertBean(webNoticeReqVo, WebNotice.class);
        LambdaQueryWrapper<WebNotice> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(webNotice.getId())) {
            queryWrapper.eq(WebNotice::getId, webNoticeReqVo.getId());
        }
        queryWrapper.orderByDesc(WebNotice::getCreateTime);
        IPage<WebNotice> selectResult = webNoticeMapper.selectPage(new Page<>(current, size, true), queryWrapper);
        List<WebNoticeRespVo> resultList = selectResult.getRecords().stream()
                .map(bean -> {
                    WebNoticeRespVo webNoticeRespVo = Convert2Bean.convertBean(bean, WebNoticeRespVo.class);
                    webNoticeRespVo.setCreateUser(userService.getById(webNoticeRespVo.getCreateUser()).getName());
                    return webNoticeRespVo;
                })
                .collect(Collectors.toList());
        Page<WebNoticeRespVo> page = new Page<>(current, size, true);
        page.setCurrent(selectResult.getCurrent());
        page.setTotal(selectResult.getTotal());
        page.setRecords(resultList);
        return page;
    }

    @Override
    public WebNoticeRespVo queryById(String id) {
        WebNotice webNotice = baseMapper.selectById(id);
        return Convert2Bean.convertBean(webNotice, WebNoticeRespVo.class);
    }

    @Override
    public Boolean addNotice(WebNoticeReqVo webNoticeReqVo) {
        webNoticeReqVo.setId("WB" + IdUtil.simpleUUID());
        WebNotice webNotice = Convert2Bean.convertBean(webNoticeReqVo, WebNotice.class);
        return baseMapper.insert(webNotice) > 0;
    }

    @Override
    public Boolean updateNotice(WebNoticeReqVo webNoticeReqVo) {
        WebNotice webNotice = Convert2Bean.convertBean(webNoticeReqVo, WebNotice.class);
        LambdaUpdateChainWrapper<WebNotice> wrapper = new LambdaUpdateChainWrapper<>(webNoticeMapper);
        if (ObjectUtil.isNotEmpty(webNotice.getCreateUser())) {
            wrapper.set(WebNotice::getCreateUser, webNotice.getCreateUser());
        }
        wrapper.eq(WebNotice::getId, webNotice.getId());
        return wrapper.update();
    }

    @Override
    public Boolean deleteNotice(String id) {
        return webNoticeMapper.deleteById(id) > 0;
    }
}
